package com.yvon.maple.auth.authorization.exception;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.yvon.boot.common.result.ResponseData;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;

/**
 * @author: Yvon
 * @Date: 2020/6/13 19:02
 * @Description: 自定义OAuth2Exception 异常
 */
@EqualsAndHashCode(callSuper = true)
@Data
@JsonSerialize(using = CustomOAuthExceptionJacksonSerializer.class)
public class CustomOAuth2Exception extends OAuth2Exception {

    private final ResponseData responseData;

    public CustomOAuth2Exception(String msg, Throwable t) {
        super(msg, t);
        this.responseData = ResponseData.failed(msg);
    }

    public CustomOAuth2Exception(OAuth2Exception oAuth2Exception) {
        super(oAuth2Exception.getSummary(), oAuth2Exception);
        AuthErrorType authErrorType = AuthErrorType.valueOf(oAuth2Exception.getOAuth2ErrorCode().toUpperCase());
        this.responseData = ResponseData.failed(Integer.valueOf(authErrorType.getCode()), authErrorType.getMesg());
    }
}
